import java.util.ArrayList;
import java.util.List;

public class Compress {

    public int compress(char[] chars) {
        if (chars.length == 1) return 1;
        int l = 0;
        int r = 1;
        int count = 1;
        char temp = chars[0];
        while (r < chars.length) {
            if (temp == chars[r]) {
                ++count;
                ++r;
            } else {
                chars[l++] = temp;
                if (count != 1) {
                    List<Character> list = new ArrayList<>();
                    while (count > 0) {
                        list.add((char) (count % 10 + 48));
                        count /= 10;
                    }
                    for (int i = list.size() - 1; i >= 0; i--) {
                        chars[l++] = list.get(i);
                    }
                    count = 1;
                }
                temp = chars[r];
                ++r;
            }
        }
        chars[l++] = temp;
        if (count != 1) {
            List<Character> list = new ArrayList<>();
            while (count > 0) {
                list.add((char) (count % 10 + 48));
                count /= 10;
            }
            for (int i = list.size() - 1; i >= 0; i--) {
                chars[l++] = list.get(i);
            }
        }
        return l;
    }
}
